﻿#region File Description
/****************************************************************************
*                                                                           *
* Game Manager.                                                             *
*                                                                           *
* Copyright (c) Ionescu Marius. All rights reserved(2013).                  *
*                                                                           *
****************************************************************************/
#endregion
namespace Silverlight3D
{
    using System;

    /// <summary>
    /// A 3D Vector.  May represent a Position or a Velocity (both are 3D Vectors)
    /// </summary>
    public class Vector
    {
        #region Properties

        public double X { get; set; }
        public double Y { get; set; }
        public double Z { get; set; }

        #endregion

        #region Constructors

        public Vector()
        {
            X = 0;
            Y = 0;
            Z = 0;
        }

        public Vector(double x, double y, double z)
        {
            X = x;
            Y = y;
            Z = z;
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Returns the square of the magnitude of the Vector (Pythagoras theorem).
        /// Slightly cheaper than Magnitude() as there is no square root.
        /// </summary>
        /// <returns>Square of the magnitude of the Vector.</returns>
        public double MagnitudeSquared()
        {
            return X * X + Y * Y + Z * Z;
        }

        /// <summary>
        /// Returns the magnitude of the Vector (Pythagoras theorem)
        /// </summary>
        /// <returns>Magnitude of the Vector.</returns>
        public double Magnitude()
        {
            return Math.Sqrt(MagnitudeSquared());
        }

        /// <summary>
        /// Subtracts one Vector from another.
        /// </summary>
        /// <returns>Difference between the two Vectors.</returns>
        public static Vector operator-(Vector v1, Vector v2)
        {
            Vector result = new Vector();

            result.X = v1.X - v2.X;
            result.Y = v1.Y - v2.Y;
            result.Z = v1.Z - v2.Z;

            return result;
        }

        /// <summary>
        /// Adds one Vector to another.
        /// </summary>
        /// <returns>The sum of the two Vectors.</returns>
        public static Vector operator +(Vector v1, Vector v2)
        {
            Vector result = new Vector();

            result.X = v1.X + v2.X;
            result.Y = v1.Y + v2.Y;
            result.Z = v1.Z + v2.Z;

            return result;
        }

        /// <summary>
        /// Multiplies a Vector by a factor.
        /// </summary>
        /// <returns>the Vector multiplied by the factor.</returns>
        public static Vector operator *(Vector v1, double factor)
        {
            Vector result = new Vector();

            result.X = v1.X * factor;
            result.Y = v1.Y * factor;
            result.Z = v1.Z * factor;

            return result;
        }

        public Vector AddX(double dx)
        {
            Vector result = this.Clone();

            result.X += dx;

            return result;
        }

        public Vector AddY(double dy)
        {
            Vector result = this.Clone();

            result.Y += dy;

            return result;
        }

        public Vector AddZ(double dz)
        {
            Vector result = this.Clone();

            result.Z += dz;

            return result;
        }

        public Vector Clone()
        {
            Vector result = new Vector();

            result.X = this.X;
            result.Y = this.Y;
            result.Z = this.Z;

            return result;
        }

        #endregion
    }
}